package com.itheima.reggie.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    /**
     * 保存套餐和菜品
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐基本数据
        this.save(setmealDto);

        //保存套餐中菜品数据
        //获得菜品集合
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //给菜品集合中的所属套餐id,赋值,setmealDto.getId()
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        //保存套餐菜品集合
        setmealDishService.saveBatch(setmealDishes);

    }

    //通过id更改售卖状态
    @Override
    @Transactional
    public int deleteByIds(Long[] ids) {
        int count = 0;
        //遍历
        for (Long id : ids) {
            //判断套餐是否停售
            Integer status = this.getById(id).getStatus();
            if (status == 1){
                continue;
            }
            count ++;
            //删除套餐
            this.removeById(id);
            //删除套餐菜品
            setmealDishService.removeById(id);
        }

        return count;
    }

    /**
     * 修改
     */
    // 1.回显数据
    @Override
    public SetmealDto updateAll(Long id) {
        SetmealDto setmealDto = new SetmealDto();

        Setmeal setmeal = this.getById(id);

        LambdaQueryWrapper<SetmealDish> eq = Wrappers.lambdaQuery(SetmealDish.class)
                .eq(id != null, SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(eq);

        setmealDto.setSetmealDishes(list);
        BeanUtils.copyProperties(setmeal,setmealDto);
        return setmealDto;
    }
    // 2.修改数据
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //更新基本表
        this.updateById(setmealDto);
        //清除套餐菜品信息,删除所有与本套餐id相同id的菜品
        LambdaUpdateWrapper<SetmealDish> eq = Wrappers.lambdaUpdate(SetmealDish.class)
                .eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(eq);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }
}
